<?php
class singlecontent extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('modsinglecontent');
		$this->load->model('modsetmenu');
	}
	
	function index()
	{
		$config['base_url'] = base_url().'singlecontent/index/';
		$config['total_rows'] = $this->db->count_all('single_content');
		$config['per_page'] = 15;
		$config['uri_segment'] = 3;
		
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';

		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		
		$token = $this->session->userdata('token');
		if($token){
			$data['results'] = $this->modsinglecontent->getList($config['per_page'],$this->uri->segment(3));
			$this->load->view('singlecontent/list',$data);
		}else{
			redirect('login');
		}
	}
    
	function add(){
		$data['mode'] = "Tambah Content";
		$data['action'] = "save";
		$data['menucontent'] = $this->modsetmenu->getComboMenuByTypeMenu(1);
		$this->load->view('singlecontent/form',$data);
	}
	
	function edit($id_content){
		$data['mode'] = "Ubah Content";
		$data['action'] = "update";
		$data['detail'] = $this->modsinglecontent->detail($id_content);
		$data['menucontent'] = $this->modsetmenu->getComboMenuByTypeMenu(1);
		$this->load->view('singlecontent/form',$data);
	}
	
	function save()
	{
		$id_content = $this->input->post('id_content');
		$id_menu = $this->input->post('id_menu');
		$title_content = $this->input->post('title_content');
		$date_content = date('Y-m-d');
		$container_content = $this->input->post('container_content');
		$enable = $this->input->post('enable');
		
		$config['upload_path'] = './images/uploads/content_files';
		$config['allowed_types'] = 'pdf|docx|doc|png|jpeg|jpg|gif';
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('file_upload');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$file_upload="";
			}
			else{
				$file_upload = $getFile['file_name'];
			}
		}else{
			$file_upload = $this->input->post('file_upload_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if($submit)
		{
			$this->modsinglecontent->setData($id_content,$id_menu,$title_content,$date_content,$container_content,$file_upload,$enable);
			$this->modsinglecontent->create();
			$this->session->set_flashdata('success', true);
			redirect('singlecontent');
		}
		$this->session->set_flashdata('error', true);
		redirect('singlecontent');
	}
	
	function update()
	{
		$id_content = $this->input->post('id_content');
		$id_menu = $this->input->post('id_menu');
		$title_content = $this->input->post('title_content');
		$date_content = $this->input->post('date_content');
		$container_content = $this->input->post('container_content');
		$enable = $this->input->post('enable');
		
		$config['upload_path'] = './images/uploads/content_files';
		$config['allowed_types'] = 'pdf|docx|doc|png|jpeg|jpg|gif';
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('file_upload');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$file_upload="";
			}
			else{
				$file_upload = $getFile['file_name'];
			}
		}else{
			$file_upload = $this->input->post('file_upload_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->modsinglecontent->setData($id_content,$id_menu,$title_content,$date_content,$container_content,$file_upload,$enable);
			$this->modsinglecontent->update($id_content);
			$this->session->set_flashdata('success', true);
			redirect('singlecontent');
		}
		$this->session->set_flashdata('error', true);
		redirect('singlecontent');
	}
	
	function delete($id_content)
	{
		$getHapus = $this->modsinglecontent->detail($id_content);
		@unlink("images/uploads/content_files/".$getHapus[0]['file_upload']);
		
		if ($this->modsinglecontent->remove($id_content)){
			$this->session->set_flashdata('delete', true);
			redirect('singlecontent','refresh');
		}
		$this->session->set_flashdata('error', true);
		redirect('singlecontent');
	}	
	
	function searchData(){
		$name = $this->input->post('name');
		$data['results'] = $this->modsinglecontent->getListSearch($name);
		$this->load->view('singlecontent/list',$data);
	}
}
?>